#include "subscribedialog.h"
#include "ui_subscribedialog.h"
#include "completers.h"

SubscribeDialog::SubscribeDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SubscribeDialog)
{
    ui->setupUi(this);
    QCompleter *nameCompleter = new QCompleter(Completers::nameModel(), this);
    nameCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    nameCompleter->setCompletionMode(QCompleter::PopupCompletion);
    nameCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
    ui->nameEdit->setCompleter(nameCompleter);
    m_urlCompleter = new QCompleter(Completers::urlModel(), this);
    m_urlCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    m_urlCompleter->setCompletionMode(QCompleter::PopupCompletion);
    m_urlCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
    ui->urlEdit->setCompleter(m_urlCompleter);
}

QString SubscribeDialog::name() const {
    return ui->nameEdit->text();
}

QString SubscribeDialog::url() const {
    return ui->urlEdit->text();
}

SubscribeDialog::~SubscribeDialog()
{
    delete ui;
}

void SubscribeDialog::on_nameEdit_lostFocus()
{
    QString name = ui->nameEdit->text();
    QString url = Completers::map()[name].toString();
    if (!url.isEmpty())
        ui->urlEdit->setText(url);
}
